/*
 * @Date: 2025-05-06 09:21:39
 * @LastEditors: lyx
 * @LastEditTime: 2025-05-16 11:10:40
 * @FilePath: \tpt-cnpc\src\pages\BigScreen\components\MainBody\index.tsx
 * @Description: 
 */
import { useContext, type FC } from "react";
import styles from "./index.module.less";
import BorderCard from "../BorderCard";
import { BigScreenContext } from "../../context/BigScreenContext";
import useBody from "./hooks/useBody";
import Conversation from "@/components/Conversation";
import cName from "classnames";
import ChatRightArea from "./components/ChatRightArea";
import { useEffect } from "react";

const MainBody: FC = () => {
  const bigScreenContext = useContext(BigScreenContext);
  const { chatFull, chatRef, chartFull, shouldKeepZIndex, setShouldKeepZIndex } = bigScreenContext;
  const body = useBody();

  const handleTransitionEnd = () => {
    if (!chatFull) {
      setShouldKeepZIndex(false); // 收缩动画结束后移除 z-index
    }
  };

  useEffect(() => {
    if (chatFull) {
      setShouldKeepZIndex(true);
    }
  }, [chatFull])

  return (
    <div className={cName(styles["main-body-container"], {
      [styles["full"]]: chatFull || chartFull || shouldKeepZIndex,
    })}>
      <div
        className={cName(styles["left-comp"], {
          [styles["hidden"]]: chatFull,
        })}
      >
        <BorderCard height="100%" title={body.body?.[bigScreenContext.currentKey ?? ""]?.title ?? ""}>
          {bigScreenContext.currentKey &&
            body.body[bigScreenContext.currentKey]?.content}
        </BorderCard>
      </div>
      <div
        className={cName(styles["right-comp"], {
          [styles["full"]]: chatFull,
        })}
        onTransitionEnd={handleTransitionEnd}
      >
        <BorderCard
          height="100%"
          title="智能体"
          operArea={
            <ChatRightArea
              handleNewChat={() => {
                chatRef?.current?.handleNewChat?.();
              }}
            />
          }
        >
          <Conversation ref={chatRef} />
        </BorderCard>
      </div>
    </div>
  );
};

export default MainBody;
